package com.zhuozhou.jdyc.main.main.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.base.BaseRecyclerViewAdapter;
import com.zhuozhou.jdyc.base.BaseViewHolder;
import com.zhuozhou.jdyc.databinding.TabItemMenuBinding;
import com.zhuozhou.jdyc.main.main.bean.TabItemBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by CherishTang on 2020/3/27 0027
 * describe:首页菜单adapter
 */
public class IndexTabMenuAdapter extends BaseRecyclerViewAdapter<TabItemBean, TabItemMenuBinding> {
    int width = 0;
    int color = 0;
    public OnTabItemMenuClickListener mOnTabItemMenuClickListener;

    public IndexTabMenuAdapter(Context context) {
        super(context);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.tab_item_menu;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder baseViewHolder, int pos) {
        super.onBindViewHolder(baseViewHolder, pos);
    }

    @Override
    public void onBindHolder(BaseViewHolder<TabItemMenuBinding> holder, int pos) {
        if (width != 0)
            holder.getBinding().tvTop.setWidth(width);
        if (color != 0)
            holder.getBinding().tvTop.setTextColor(mContext.getResources().getColor(color));
        holder.getBinding().tvTop.setText(mList.get(pos).getTopName());
        if (mList.get(pos).getIsView()) {
            holder.getBinding().vBottom.setVisibility(View.VISIBLE);
        } else {
            holder.getBinding().vBottom.setVisibility(View.GONE);
        }
        holder.getBinding().getRoot().setOnClickListener(v -> {
            List<TabItemBean> list = new ArrayList<>();
            for (TabItemBean item : mList) {
                if (item.getIsView()) {
                    item.setIsView(false);
                }
                list.add(item);
            }
            list.get(pos).setIsView(true);
            setList(list);
            notifyDataSetChanged();
            if (mOnTabItemMenuClickListener != null) {
                mOnTabItemMenuClickListener.onTabItemMenuClick(holder.getBinding().tvTop, pos);
            }
        });
    }

    public void setOnTabMenuItemClickListener(OnTabItemMenuClickListener l) {
        mOnTabItemMenuClickListener = l;
    }

    public interface OnTabItemMenuClickListener {
        void onTabItemMenuClick(TextView view, int position);
    }

    public void setViewWidth(int width) {
        this.width = width;
    }
    public void setTextColor(int color) {
        this.color = color;
    }
}
